package com.xxx.ware.ware_manage_sys.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * 解决跨域问题
 */
@Configuration
public class CorsConfig {

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);    //设置允许跨域
//        config.addAllowedOrigin("*"); // 允许任何域进行跨域请求 Springboot2.4版本后不可用
        config.addAllowedOriginPattern("*"); //允许任何域进行跨域请求 Springboot2.4版本后用此配置
        config.addAllowedHeader("*"); // 允许任何头
        config.addAllowedMethod("*"); // 允许任何方法（如GET, POST, PUT, DELETE等）
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }
}
